package com.ruoyi.web.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.Goods;
import com.ruoyi.business.service.IGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 货品信息Controller
 * 
 * @author ruoyi
 * @date 2025-03-20
 */
@RestController
@RequestMapping("/business/goods")
public class GoodsController extends BaseController
{
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询货品信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(Goods goods)
    {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }

    /**
     * 导出货品信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:goods:export')")
    @Log(title = "货品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Goods goods)
    {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.exportExcel(response, list, "货品信息数据");
    }

    /**
     * 获取货品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(goodsService.selectGoodsById(id));
    }

    /**
     * 新增货品信息
     */
    @PreAuthorize("@ss.hasPermi('business:goods:add')")
    @Log(title = "货品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Goods goods)
    {
        Long userId = getLoginUser().getUser().getUserId();
        System.out.println(userId);
        goods.setUserId(userId);
        return toAjax(goodsService.insertGoods(goods));
    }

    /**
     * 修改货品信息
     */
    @PreAuthorize("@ss.hasPermi('business:goods:edit')")
    @Log(title = "货品信息", businessType = BusinessType.UPDATE)
    @PutMapping("")
    public AjaxResult edit(@RequestBody Goods goods)
    {

        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除货品信息
     */
    @PreAuthorize("@ss.hasPermi('business:goods:remove')")
    @Log(title = "货品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(goodsService.deleteGoodsByIds(ids));
    }


    /**
     * 导入货品信息
     */
    @PreAuthorize("@ss.hasPermi('business:goods:import')")
    @Log(title = "货品信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        if (!file.isEmpty())
        {
            ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
            List<Goods> importList = util.importExcel(file.getInputStream());


            String operName = getUsername();
            String message = goodsService.importGoods(importList, updateSupport, operName);
            return success(message);
        }
        return error("请选择要导入的数据文件！");
    }

    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.importTemplateExcel(response, "货品数据");
    }
}
